package com.leyou.goods.item.service;

import com.leyou.goods.item.dao.SpecParamDao;
import com.leyou.goods.item.dao.SpecificationDao;
import com.leyou.goods.item.pojo.SpecGroup;
import com.leyou.goods.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SpecificationService {

    @Autowired(required = false)
    private SpecificationDao groupMapper;
    @Autowired(required = false)
    private SpecParamDao paramMapper;

    /**
     * 根据分类id查询分组
     * @param cid
     * @return
     */
    public List<SpecGroup> queryGroupsByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        List<SpecGroup> select = this.groupMapper.select(specGroup);
        System.out.println(select.toString());
        return select;
    }
    /**
     * 根据gid查询规格参数
     * @param gid
     * @return
     */
    public List<SpecParam> queryParams(Long gid, Long cid, Boolean generic, Boolean searching) {
        SpecParam record = new SpecParam();
        record.setGroupId(gid);
        record.setCid(cid);
        record.setGeneric(generic);
        record.setSearching(searching);
        return this.paramMapper.select(record);
    }
    public void addGroup(SpecGroup specGroup){
        groupMapper.insert(specGroup);
    }

    public void addParam(SpecParam specGroup) {
        paramMapper.insert(specGroup);
    }

    public void deleteGroup(Long id){
        groupMapper.deleteByPrimaryKey(id);
    }

    public void deleteParam(Long id) {
        System.out.println("++++++++++++++++++++++++++++++++++++++++"+id);
        paramMapper.deleteByPrimaryKey(id);
    }
    public List<SpecGroup> querySpecsByCid(Long cid) {
        // 查询规格组
        List<SpecGroup> groups = this.queryGroupsByCid(cid);
        groups.forEach(g -> {
            // 查询组内参数
            g.setParams(this.queryParams(g.getId(), null, null, null));
        });
        return groups;
    }
}
